<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017-7-13
 * Time: 16:17
 */

namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use think\Db;

class VideoController extends AdminBaseController
{

    public function index()
    {
        $param    = $this->request->param();
        $s_name = isset($param['s_name']) ? $param['s_name'] : '';
        $where['l.l_status'] = 1;
        if($s_name){ $where['sl.s_name'] = ['like', "%$s_name%"];}else{ unset($param['s_name']);}
        $live = Db::name('live')->field('l.id,sl.s_name,l.l_like,l.l_viewing,l.l_click,l.l_share,l.l_content,l.l_top,l.l_status,SEC_TO_TIME(TIMESTAMPDIFF(SECOND,l_createtime,NOW())) as l_createtime')
            ->alias('l')
            ->join('star_list sl','l.l_udid = sl.s_udid')
            ->where($where)
            ->order(['l.l_top'=>'ASC','l.l_createtime' => 'DESC','l.id' => 'DESC'])
            ->paginate(20);
        $live->appends($param);
        $page = $live->render();
        $this->assign("s_name", $s_name);
        $this->assign("live", $live);
        $this->assign("page", $page);
        return $this->fetch();
    }

    public function abnormal()
    {
        $param    = $this->request->param();
        $s_name = isset($param['s_name']) ? $param['s_name'] : '';
        $where['l.l_status'] = 2;
        if($s_name){ $where['sl.s_name'] = ['like', "%$s_name%"];}else{ unset($param['s_name']);}
        $live = Db::name('live')->field('l.id,sl.s_name,l.l_like,l.l_viewing,l.l_click,l.l_share,l.l_content,l.l_top,l.l_status')
            ->alias('l')
            ->join('star_list sl','l.l_udid = sl.s_udid')
            ->where($where)
            ->order(['l.l_top'=>'ASC','l.l_createtime' => 'DESC','l.id' => 'DESC'])
            ->paginate(20);
        $live->appends($param);
        $page = $live->render();
        $this->assign("s_name", $s_name);
        $this->assign("live", $live);
        $this->assign("page", $page);
        return $this->fetch();
    }

    public function playback()
    {
        $param    = $this->request->param();
        $s_name = isset($param['s_name']) ? $param['s_name'] : '';
        $where['l.l_status'] = 9;
        if($s_name){ $where['sl.s_name'] = ['like', "%$s_name%"];}else{ unset($param['s_name']);}
        $live = Db::name('live')->field('l.id,sl.s_name,l.l_like,l.l_viewing,l.l_click,l.l_share,l.l_content,l.l_top,l.l_status')
            ->alias('l')
            ->join('star_list sl','l.l_udid = sl.s_udid')
            ->where($where)
            ->order(['l.l_top'=>'ASC','l.l_createtime' => 'DESC','l.id' => 'DESC'])
            ->paginate(20);
        $live->appends($param);
        $page = $live->render();
        $this->assign("s_name", $s_name);
        $this->assign("live", $live);
        $this->assign("page", $page);
        return $this->fetch();
    }

    public function disabled()
    {
        $id    = $this->request->param('id', 0, 'intval');
        $live = DB::name('live')->where(["id" => $id])->find();
        $data['channel'] = $live['l_channel'];
        $url = 'Callbacks/closeRoom';
        $this->httpPost(HTTP_WEB.$url,$data);
        DB::name('live')->where(["id" => $id])->setField('l_status', 3);
        $this->success('直播已关闭');
    }

    public function top()
    {
        $id    = $this->request->param('id', 0, 'intval');
        $shout = DB::name('live')->where(["id" => $id])->find();
        switch ($shout['l_top']){
            case 10:
                DB::name('live')->where(["id" => $id])->setField('l_top', 20);
                $this->success('您已取消置顶此预播');
                break;
            case 20:
                DB::name('live')->where(["id" => $id])->setField('l_top', 10);
                $this->success('您已置顶此预播');
                break;
            default:
                DB::name('live')->where(["id" => $id])->setField('l_top', 20);
                $this->error('未知的预播置顶状态,已置为取消置顶');
        }
    }
}